﻿using ACC.Data.Entities.Client;
using ACC.Data.Entities.Common;
using ACC.Extensions.Entities;
using ACC.Extensions.Utilities;
using iPlant.Framework.ACC.WCF.WCF;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ACC.Extensions.ClientBusiness
{
    public class AccProcess
    {
        public ClientConfig AccClient { get; set; }
        public ACCWcfClient WcfClient { get; set; }
        public bool IsConnected { get; private set; }
        public bool ChangeoverPerUnload { get; set; }  //标识是否需要每次Unload都要换型

        public AccProcess()
        {
            InitProcess();
        }


        /// 初始化行为属性，在脚本中可以改写
        /// </summary>
        public virtual void InitProcess()
        {

        }


        public virtual bool Connect(ClientConfig pcClient)
        {
            try
            {
                IsConnected = false;
                AccClient = pcClient;
                //初始化WCF的Client
                if (pcClient != null && !String.IsNullOrEmpty(pcClient.ServerIP.Trim()) && pcClient.ServerPort > 0)
                {
                    string endpoint = String.Format("http://{0}:{1}/Interface", pcClient.ServerIP, pcClient.ServerPort);
                    WcfClient = new ACCWcfClient(endpoint);
                    IsConnected = true;
                }
            }
            catch (Exception ex)
            {
                Log4Helper.Error("Connect ACC WCF Error: ", ex);
            }
            return IsConnected;
        }

        /// <summary>
        /// 条码检查，要早于PreLoad执行
        /// </summary>
        /// <param name="barcodes"></param>
        /// <returns></returns>
        public virtual ClientResponse Validate(string barcode)
        {
            return new ClientResponse { Result = true };
        }

        /// 在Unload之前会调用，方便用户重写，在Unload之前做一些事
        /// </summary>
        /// <param name="barcodes"></param>
        /// <param name="processDatas"></param>
        /// <param name="statusBit"></param>
        /// <returns></returns>
        public virtual ClientResponse PreLoad(List<string> barcodes, int partTypeId, List<ClientProcData> processDatas = null)
        {
            ClientResponse accResult = new ClientResponse();
            if (AccClient == null || WcfClient == null || !IsConnected)
            {
                accResult.Result = false;
                accResult.Message = "ACC Server disconnected";
                return accResult;
            }
            Dictionary<string, object> variables = new Dictionary<string, object>();
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("Barcodes", barcodes);
            parameters.Add("Line", AccClient.Line);
            parameters.Add("OP", AccClient.OP);
            parameters.Add("STN", AccClient.STN);
            parameters.Add("PartTypeId", partTypeId);

            if (processDatas != null)
                parameters.Add("ProcDatas", processDatas);

            ClientRequest cmd = new ClientRequest
            {
                CmdName = "PreLoad",
                Parameters = parameters,
                Variables = variables
            };
            Log4Helper.Info(JsonHelper.ToJson(cmd));
            ClientResponse result = WcfClient.ExecuteCmd(cmd);
            Log4Helper.Info(JsonHelper.ToJson(result));
            accResult.Result = result.Result;
            accResult.Message = result.Message;
            accResult.Data = result.Data == null ? "" : result.Data.ToString();

            if (OnPreLoad != null)
            {
                OnPreLoad(barcodes, accResult);
            }

            return accResult;
        }

        /// <summary>
        /// 在Unload之前会调用，方便用户重写，在Unload之前做一些事
        /// </summary>
        /// <param name="barcodes"></param>
        /// <param name="processDatas"></param>
        /// <param name="statusBit"></param>
        /// <returns></returns>
        public virtual ClientResponse PreUnload(List<string> barcodes, List<ClientProcData> processDatas, int statusBit, int partTypeId = 0)
        {
            return new ClientResponse { Result = true };
        }

        /// 调用ACC的Load操作
        /// </summary>
        /// <param name="barcodes"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public virtual ClientResponse Load(List<string> barcodes, int partTypeId, List<ClientProcData> processDatas = null)
        {
            ClientResponse accResult = new ClientResponse();
            if (AccClient == null || WcfClient == null || !IsConnected)
            {
                accResult.Result = false;
                accResult.Message = "ACC Server disconnected";
                return accResult;
            }
            //while(IsConnected ==false)
            //{
            //    Thread.Sleep(100);
            //}
            Dictionary<string, object> variables = new Dictionary<string, object>();
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("Barcodes", barcodes);
            parameters.Add("Line", AccClient.Line);
            parameters.Add("OP", AccClient.OP);
            parameters.Add("STN", AccClient.STN);
            parameters.Add("PartTypeId", partTypeId);

            if (processDatas != null)
                parameters.Add("ProcDatas", processDatas);

            ClientRequest cmd = new ClientRequest
            {
                CmdName = "Load",
                Parameters = parameters,
                Variables = variables
            };
            Log4Helper.Info(JsonHelper.ToJson(cmd));
            ClientResponse result = WcfClient.ExecuteCmd(cmd);
            Log4Helper.Info(JsonHelper.ToJson(result));
            accResult = new ClientResponse();
            accResult.Result = result.Result;
            accResult.Message = result.Message;
            accResult.Data = result.Data;
            if (OnLoad != null)
            {
                OnLoad(barcodes, accResult);
            }

            return accResult;
        }
        /// 调用ACC的Load操作,上传Hardness
        /// </summary>
        /// <param name="barcodes"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public virtual ClientResponse Load(string strBarcode, List<ClientProcData> listProcData)
        {
            ClientResponse accResult = new ClientResponse();
            if (AccClient == null || WcfClient == null || !IsConnected)
            {
                accResult.Result = false;
                accResult.Message = "ACC Server disconnected";
                return accResult;
            }
            //while (IsConnected == false)
            //{
            //    Thread.Sleep(100);
            //}
            Dictionary<string, object> variables = new Dictionary<string, object>();
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("Barcodes", new List<string>() { strBarcode });
            parameters.Add("Line", AccClient.Line);
            parameters.Add("OP", AccClient.OP);
            parameters.Add("STN", AccClient.STN);
            parameters.Add("PartTypeId", "");

            if (listProcData != null)
                parameters.Add("ProcDatas", listProcData);

            ClientRequest cmd = new ClientRequest
            {
                CmdName = "Load",
                Parameters = parameters,
                Variables = variables
            };
            Log4Helper.Info(JsonHelper.ToJson(cmd));
            ClientResponse result = WcfClient.ExecuteCmd(cmd);
            Log4Helper.Info(JsonHelper.ToJson(result));
            accResult = new ClientResponse();
            accResult.Result = result.Result;
            accResult.Message = result.Message;
            accResult.Data = result.Data;
            return accResult;
        }
        /// 调用ACC的Load操作,上传铸造机数据
        /// </summary>
        /// <param name="barcodes"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public virtual ClientResponse Load(List<ClientProcData> listProcData)
        {
            ClientResponse accResult = new ClientResponse();
            if (AccClient == null || WcfClient == null || !IsConnected)
            {
                accResult.Result = false;
                accResult.Message = "ACC Server disconnected";
                return accResult;
            }
            //while (IsConnected == false)
            //{
            //    Thread.Sleep(100);
            //}
            Dictionary<string, object> variables = new Dictionary<string, object>();
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("Barcodes", new List<string>() { "XLL1234", "XLL1234", "XLL1234", "XLL1234" });
            parameters.Add("Line", AccClient.Line);
            parameters.Add("OP", AccClient.OP);
            parameters.Add("STN", AccClient.STN);
            parameters.Add("PartTypeId", "");

            if (listProcData != null)
                parameters.Add("ProcDatas", listProcData);

            ClientRequest cmd = new ClientRequest
            {
                CmdName = "Load",
                Parameters = parameters,
                Variables = variables
            };
            Log4Helper.Info(JsonHelper.ToJson(cmd));
            ClientResponse result = WcfClient.ExecuteCmd(cmd);
            Log4Helper.Info(JsonHelper.ToJson(result));
            accResult = new ClientResponse();
            accResult.Result = result.Result;
            accResult.Message = result.Message;
            accResult.Data = result.Data;
            return accResult;
        }

        /// 调用ACC的unload操作
        /// </summary>
        /// <param name="barcodes"></param>
        /// <param name="processDatas"></param>
        /// <param name="statusBit"></param>
        /// <returns></returns>
        public virtual ClientResponse Unload(List<string> barcodes, List<ClientProcData> processDatas, UnloadVariable unloadData, int statusBit, int failureBits, int partTypeId)
        {
            ClientResponse accResult = new ClientResponse();
            if (AccClient == null || WcfClient == null || !IsConnected)
            {
                accResult.Result = false;
                accResult.Message = "ACC Server disconnected";
                return accResult;
            }

            accResult = PreUnload(barcodes, processDatas, statusBit);

            if (!accResult.Result)
            {
                return accResult;
            }

            Dictionary<string, object> variables = new Dictionary<string, object>();
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("Barcodes", barcodes);
            parameters.Add("Line", AccClient.Line);
            parameters.Add("OP", AccClient.OP);
            parameters.Add("STN", AccClient.STN);
            parameters.Add("StatusBits", statusBit);
            parameters.Add("FailureBits", failureBits);
            parameters.Add("ProcDatas", processDatas);

            if (unloadData != null)
            {
                variables.Add("IsTestUnit", unloadData.IsTestUnit);
            }
            else
            {
                variables.Add("IsTestUnit", null);
            }
            variables.Add("IsStartUp", unloadData == null ? false : unloadData.IsStartUp);
            variables.Add("IsHold", unloadData == null ? false : unloadData.IsHold);
            variables.Add("FailureBits", unloadData == null ? null : unloadData.LstStatusBit);

            ClientRequest cmd = new ClientRequest
            {
                CmdName = "Unload",
                Parameters = parameters,
                Variables = variables
            };
            Log4Helper.Info(JsonHelper.ToJson(cmd));
            ClientResponse result = WcfClient.ExecuteCmd(cmd);
            Log4Helper.Info(JsonHelper.ToJson(result));
            accResult.Result = result.Result;
            accResult.Message = result.Message;
            accResult.Data = result.Data;
            if (OnUnload != null)
            {
                OnUnload(barcodes, accResult);
            }
            return accResult;
        }

        /// 调用ACC的unload操作
        /// </summary>
        /// <param name="barcodes"></param>
        /// <param name="processDatas"></param>
        /// <param name="statusBit"></param>
        /// <returns></returns>
        public virtual ClientResponse Unload(List<string> barcodes, List<ClientProcData> processDatas)
        {
            ClientResponse accResult = new ClientResponse();
            if (AccClient == null || WcfClient == null || !IsConnected)
            {
                accResult.Result = false;
                accResult.Message = "ACC Server disconnected";
                return accResult;
            }

            Dictionary<string, object> variables = new Dictionary<string, object>();
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("Barcodes", barcodes);
            parameters.Add("Line", AccClient.Line);
            parameters.Add("OP", AccClient.OP);
            parameters.Add("STN", AccClient.STN);
            parameters.Add("ProcDatas", processDatas);

            ClientRequest cmd = new ClientRequest
            {
                CmdName = "Unload",
                Parameters = parameters,
                Variables = variables
            };
            Log4Helper.Info(JsonHelper.ToJson(cmd));
            ClientResponse result = WcfClient.ExecuteCmd(cmd);
            Log4Helper.Info(JsonHelper.ToJson(result));
            accResult.Result = result.Result;
            accResult.Message = result.Message;

            if (OnUnload != null)
            {
                OnUnload(barcodes, accResult);
            }
            return accResult;
        }

        public virtual ClientOperator OperatorLogin(string operatorId, string op, string stn, out string msg)
        {
            Dictionary<string, object> variables = new Dictionary<string, object>();
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("Line", AccClient.Line);
            parameters.Add("OP", op);
            parameters.Add("STN", stn);
            parameters.Add("OperatorId", operatorId);
            ClientRequest cmd = new ClientRequest
            {
                CmdName = "OperatorLogin",
                Parameters = parameters,
                Variables = variables
            };
            Log4Helper.Info(JsonHelper.ToJson(cmd));
            ClientResponse result = WcfClient.ExecuteCmd(cmd);
            Log4Helper.Info(JsonHelper.ToJson(result));
            if (result.Result)
            {
                msg = String.Empty;
                if (!String.IsNullOrEmpty(result.Data.ToString()))
                {
                    var oper = JsonHelper.ToObject<ClientOperator>(result.Data.ToString());
                    return oper;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                msg = result.Message;
                return null;
            }
        }

        public virtual ClientOperator OperatorLogin(string operatorId, out string msg)
        {
            Dictionary<string, object> variables = new Dictionary<string, object>();
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("Line", AccClient.Line);
            parameters.Add("OP", AccClient.OP);
            parameters.Add("STN", AccClient.STN);
            parameters.Add("OperatorId", operatorId);
            ClientRequest cmd = new ClientRequest
            {
                CmdName = "OperatorLogin",
                Parameters = parameters,
                Variables = variables
            };
            Log4Helper.Info(JsonHelper.ToJson(cmd));
            ClientResponse result = WcfClient.ExecuteCmd(cmd);
            Log4Helper.Info(JsonHelper.ToJson(result));
            if (result.Result)
            {
                msg = String.Empty;
                if (!String.IsNullOrEmpty(result.Data.ToString()))
                {
                    var oper = JsonHelper.ToObject<ClientOperator>(result.Data.ToString());
                    return oper;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                msg = result.Message;
                return null;
            }
        }

        public virtual ClientResponse OperatorCheck(string operatorId, string type)
        {
            ClientResponse accResult = new ClientResponse();
            if (AccClient == null || WcfClient == null || !IsConnected)
            {
                accResult.Result = false;
                accResult.Message = "ACC Server disconnected";
                return accResult;
            }
            Dictionary<string, object> variables = new Dictionary<string, object>();
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("OperatorId", operatorId);
            parameters.Add("Type", type);
            parameters.Add("Line", AccClient.Line);
            parameters.Add("OP", AccClient.OP);
            parameters.Add("STN", AccClient.STN);
            ClientRequest cmd = new ClientRequest
            {
                CmdName = "OperatorCheck",
                Parameters = parameters,
                Variables = variables
            };
            Log4Helper.Info(JsonHelper.ToJson(cmd));
            ClientResponse result = WcfClient.ExecuteCmd(cmd);
            Log4Helper.Info(JsonHelper.ToJson(result));
            accResult = new ClientResponse();
            accResult.Result = result.Result;
            accResult.Message = result.Message;

            return accResult;
        }

        public virtual List<ClientStnCfg> GetOperatorStationList(string operatorId, out string msg)
        {
            Dictionary<string, object> variables = new Dictionary<string, object>();
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("Line", AccClient.Line);
            parameters.Add("OperatorId", operatorId);
            parameters.Add("OP", AccClient.OP);
            parameters.Add("STN", AccClient.STN);
            ClientRequest cmd = new ClientRequest
            {
                CmdName = "OperatorStationList",
                Parameters = parameters,
                Variables = variables
            };
            Log4Helper.Info(JsonHelper.ToJson(cmd));
            ClientResponse result = WcfClient.ExecuteCmd(cmd);
            Log4Helper.Info(JsonHelper.ToJson(result));
            if (result.Result)
            {
                msg = String.Empty;
                if (!String.IsNullOrEmpty(result.Data.ToString()))
                {
                    var temp = result.Data.ToString();
                    try
                    {
                        var stations = JsonHelper.ToObject<List<ClientStnCfg>>(result.Data.ToString());
                        return stations;
                    }
                    catch (Exception ex)
                    {
                        return null;
                    }
                }
                else
                {
                    return null;
                }
            }
            else
            {
                msg = result.Message;
                return null;
            }
        }

        public virtual ClientOperator GetStationOperator()
        {
            Dictionary<string, object> variables = new Dictionary<string, object>();
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("Line", AccClient.Line);
            parameters.Add("OP", AccClient.OP);
            parameters.Add("STN", AccClient.STN);
            ClientRequest cmd = new ClientRequest
            {
                CmdName = "GetStationOperator",
                Parameters = parameters,
                Variables = variables
            };
            Log4Helper.Info(JsonHelper.ToJson(cmd));
            ClientResponse result = WcfClient.ExecuteCmd(cmd);
            Log4Helper.Info(JsonHelper.ToJson(result));
            if (result.Result)
            {
                if (result.Data != null && !String.IsNullOrEmpty(result.Data.ToString()))
                {
                    return JsonHelper.ToObject<ClientOperator>(result.Data.ToString());
                }
            }

            return null;
        }

        public virtual string GetMD5(string pwd)
        {
            System.Security.Cryptography.MD5 md5Hasher = System.Security.Cryptography.MD5.Create();
            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(pwd));
            StringBuilder sBuilder = new StringBuilder();

            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            return sBuilder.ToString();
        }

        public virtual bool GetUserCheck(string operatorId, string operatorPwd, out string msg)
        {
            msg = string.Empty;
            if (!IsConnected || AccClient == null)
            {
                msg = "ACC Server disconnected";
                return false;
            }

            Dictionary<string, object> variables = new Dictionary<string, object>();
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("Line", AccClient.Line);
            parameters.Add("OP", AccClient.OP);
            parameters.Add("STN", AccClient.STN);
            parameters.Add("OperatorId", operatorId);
            parameters.Add("OperatorPwd", GetMD5(operatorPwd));
            ClientRequest cmd = new ClientRequest
            {
                CmdName = "UserCheck",
                Parameters = parameters,
                Variables = variables
            };
            Log4Helper.Info(JsonHelper.ToJson(cmd));
            ClientResponse result = WcfClient.ExecuteCmd(cmd);
            Log4Helper.Info(JsonHelper.ToJson(result));
            if (!result.Result)
            {
                msg = result.Message;
            }

            return result.Result;
        }

        public virtual List<ClientFailureBit> GetStatusBitsList(out string msg)
        {
            Dictionary<string, object> variables = new Dictionary<string, object>();
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("Line", AccClient.Line);
            parameters.Add("OP", AccClient.OP);
            parameters.Add("STN", AccClient.STN);
            ClientRequest cmd = new ClientRequest
            {
                CmdName = "StatusBitsList",
                Parameters = parameters,
                Variables = variables
            };
            Log4Helper.Info(JsonHelper.ToJson(cmd));
            ClientResponse result = WcfClient.ExecuteCmd(cmd);
            Log4Helper.Info(JsonHelper.ToJson(result));
            if (result.Result)
            {
                msg = String.Empty;
                if (!String.IsNullOrEmpty(result.Data.ToString()))
                {
                    var list = JsonHelper.ToObject<List<ClientFailureBit>>(result.Data.ToString());
                    return list;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                msg = result.Message;
                return null;
            }
        }

        /// 型号List
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public virtual List<ClientPartType> GetPartTypeList(out string msg)
        {
            Dictionary<string, object> variables = new Dictionary<string, object>();
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("Line", AccClient.Line);
            parameters.Add("OP", AccClient.OP);
            parameters.Add("STN", AccClient.STN);
            ClientRequest cmd = new ClientRequest
            {
                CmdName = "PartTypeList",
                Parameters = parameters,
                Variables = variables
            };
            Log4Helper.Info(JsonHelper.ToJson(cmd));
            ClientResponse result = WcfClient.ExecuteCmd(cmd);
            Log4Helper.Info(JsonHelper.ToJson(result));
            if (result.Result)
            {
                msg = String.Empty;
                if (!String.IsNullOrEmpty(result.Data.ToString()))
                {
                    var list = JsonHelper.ToObject<List<ClientPartType>>(result.Data.ToString());
                    return list;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                msg = result.Message;
                return null;
            }
        }

        public virtual ClientModelChangeResult ChangeModel(int partTypeId, out string msg)
        {
            Dictionary<string, object> variables = new Dictionary<string, object>();
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("Line", AccClient.Line);
            parameters.Add("OP", AccClient.OP);
            parameters.Add("STN", AccClient.STN);
            parameters.Add("PartTypeId", partTypeId);
            ClientRequest cmd = new ClientRequest
            {
                CmdName = "ChangeModel",
                Parameters = parameters,
                Variables = variables
            };
            Log4Helper.Info(JsonHelper.ToJson(cmd));
            ClientResponse result = WcfClient.ExecuteCmd(cmd);
            Log4Helper.Info(JsonHelper.ToJson(result));
            if (result.Result)
            {
                msg = String.Empty;
                if (!String.IsNullOrEmpty(result.Data.ToString()))
                {
                    var partTypeData = JsonHelper.ToObject<ClientModelChangeResult>(result.Data.ToString());
                    return partTypeData;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                msg = result.Message;
                return null;
            }
        }

        /// <summary>
        /// 打印条码， 
        /// </summary>
        /// <param name="palletNo">托盘号</param>
        /// <returns></returns>
        public virtual ClientResponse Print(string palletNo)
        {
            Dictionary<string, object> variables = new Dictionary<string, object>();
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("Line", AccClient.Line);
            parameters.Add("OP", AccClient.OP);
            parameters.Add("STN", AccClient.STN);

            variables.Add("PalletNo", palletNo);

            ClientRequest cmd = new ClientRequest
            {
                CmdName = "Print",
                Parameters = parameters,
                Variables = variables
            };
            Log4Helper.Info(JsonHelper.ToJson(cmd));
            ClientResponse result = WcfClient.ExecuteCmd(cmd);
            Log4Helper.Info(JsonHelper.ToJson(result));

            ClientResponse accResult = new ClientResponse();
            accResult.Result = result.Result;
            accResult.Message = result.Message;
            return accResult;
        }

        /// <summary>
        /// 保存客户端配置到ACC库， 
        /// </summary>
        /// <param name="settings">配置项列表</param>
        /// <returns></returns>
        public virtual ClientResponse SaveClientCfg(List<ACCStnClientConfig> settings)
        {
            Dictionary<string, object> variables = new Dictionary<string, object>();
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("Line", AccClient.Line);
            parameters.Add("OP", AccClient.OP);
            parameters.Add("STN", AccClient.STN);

            variables.Add("ACCStnClientConfig", settings);

            ClientRequest cmd = new ClientRequest
            {
                CmdName = "SaveACCStnClientConfig",
                Parameters = parameters,
                Variables = variables
            };
            Log4Helper.Info(JsonHelper.ToJson(cmd));
            ClientResponse result = WcfClient.ExecuteCmd(cmd);
            Log4Helper.Info(JsonHelper.ToJson(result));

            ClientResponse accResult = new ClientResponse();
            accResult.Result = result.Result;
            accResult.Message = result.Message;
            return accResult;
        }

        /// <summary>
        /// Arear =0,单一的， 
        /// </summary>
        /// <param name="Arear"></param>
        /// <returns></returns>
        public virtual ClientResponse PrintLabel(string Arear)
        {
            Dictionary<string, object> variables = new Dictionary<string, object>();
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("Line", AccClient.Line);
            parameters.Add("OP", AccClient.OP);
            parameters.Add("STN", AccClient.STN);

            variables.Add("Area", Arear);

            ClientRequest cmd = new ClientRequest
            {
                CmdName = "Print",
                Parameters = parameters,
                Variables = variables
            };
            Log4Helper.Info(JsonHelper.ToJson(cmd));
            ClientResponse result = WcfClient.ExecuteCmd(cmd);
            Log4Helper.Info(JsonHelper.ToJson(result));

            ClientResponse accResult = new ClientResponse();
            accResult.Result = result.Result;
            accResult.Message = result.Message;
            if (result.Data != null)
            {
                accResult.Data = JsonHelper.ToObject<List<AreaBarcode>>(result.Data.ToString());
            }

            return accResult;
        }
        public virtual ClientResponse GetWorkOrder(DateTime date)
        {
            ClientResponse accResult = new ClientResponse();
            if (AccClient == null || WcfClient == null || !IsConnected)
            {
                accResult.Result = false;
                accResult.Message = "ACC Server disconnected";
                return accResult;
            }
            Dictionary<string, object> variables = new Dictionary<string, object>();
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("Line", AccClient.Line);
            parameters.Add("OP", AccClient.OP);
            parameters.Add("STN", AccClient.STN);
            parameters.Add("Date", date.ToString("yyyy-MM-dd"));

            ClientRequest cmd = new ClientRequest
            {
                CmdName = "GetWorkOrder",
                Parameters = parameters,
                Variables = variables
            };
            // Log4Helper.Info(JsonHelper.ToJson(cmd));
            ClientResponse result = WcfClient.ExecuteCmd(cmd);
            //  Log4Helper.Info(JsonHelper.ToJson(result));
            accResult = new ClientResponse();
            accResult.Result = result.Result;
            accResult.Message = result.Message;
            if (result.Data != null)
            {
                accResult.Data = JsonHelper.ToObject<List<ClientWorkOrder>>(result.Data.ToString());
            }
            return accResult;
        }

        public virtual ClientResponse SaveWorkOrder(DateTime date, List<ClientWorkOrder> workOrders)
        {
            ClientResponse accResult = new ClientResponse();
            if (AccClient == null || WcfClient == null || !IsConnected)
            {
                accResult.Result = false;
                accResult.Message = "ACC Server disconnected";
                return accResult;
            }

            Dictionary<string, object> variables = new Dictionary<string, object>();
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("Line", AccClient.Line);
            parameters.Add("OP", AccClient.OP);
            parameters.Add("STN", AccClient.STN);
            parameters.Add("Date", date.ToString("yyyy-MM-dd"));
            parameters.Add("WorkOrder", workOrders);

            ClientRequest cmd = new ClientRequest
            {
                CmdName = "SaveWorkOrder",
                Parameters = parameters,
                Variables = variables
            };
            Log4Helper.Info(JsonHelper.ToJson(cmd));
            ClientResponse result = WcfClient.ExecuteCmd(cmd);
            Log4Helper.Info(JsonHelper.ToJson(result));
            accResult = new ClientResponse();
            accResult.Result = result.Result;
            accResult.Message = result.Message;
            accResult.Data = result.Data;
            return accResult;
        }

        public virtual bool SaveWorkOrder(DateTime date, int qty)
        {
            Dictionary<string, object> variables = new Dictionary<string, object>();
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("Line", AccClient.Line);
            parameters.Add("OP", AccClient.OP);
            parameters.Add("STN", AccClient.STN);
            parameters.Add("Date", date.ToString("yyyy-MM-dd"));
            parameters.Add("Quantity", qty);

            ClientRequest cmd = new ClientRequest
            {
                CmdName = "UpdateWorkOrderQty",
                Parameters = parameters,
                Variables = variables
            };
            Log4Helper.Info(JsonHelper.ToJson(cmd));
            ClientResponse result = WcfClient.ExecuteCmd(cmd);
            Log4Helper.Info(JsonHelper.ToJson(result));
            return result.Result;
        }

        public virtual ClientResponse GetShiftCode()
        {
            ClientResponse accResult = new ClientResponse();
            if (AccClient == null || WcfClient == null || !IsConnected)
            {
                accResult.Result = false;
                accResult.Message = "ACC Server disconnected";
                return accResult;
            }
            Dictionary<string, object> variables = new Dictionary<string, object>();
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("Line", AccClient.Line);
            parameters.Add("OP", AccClient.OP);
            parameters.Add("STN", AccClient.STN);
            ClientRequest cmd = new ClientRequest
            {
                CmdName = "GetShiftCode",
                Parameters = parameters
            };
            ClientResponse result = WcfClient.ExecuteCmd(cmd);
            accResult.Result = result.Result;
            accResult.Message = result.Message;
            if (result.Result)
            {
                if (!String.IsNullOrEmpty(result.Data.ToString()))
                {
                    accResult.Data = result.Data.ToString();
                }
            }
            return accResult;
        }

        public virtual ClientResponse MaterialIn(string fixtureNum,int portNo,bool isCoreReq)
        {
            ClientResponse accResult = new ClientResponse();
            if (AccClient == null || WcfClient == null || !IsConnected)
            {
                accResult.Result = false;
                accResult.Message = "ACC Server disconnected";
                return accResult;
            }
            Dictionary<string, object> variables = new Dictionary<string, object>();
            variables.Add("fixtureNum", fixtureNum);
            variables.Add("portNo", portNo);
            variables.Add("nomalMaterial", !UISession.ClientSetting.IsCoreMaterial);        // 是否为三大件上料
            variables.Add("coreReq", isCoreReq);                                            // 是否为电芯叫料
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("Line", AccClient.Line);
            parameters.Add("OP", AccClient.OP);
            parameters.Add("STN", AccClient.STN);
            ClientRequest cmd = new ClientRequest
            {
                CmdName = "MaterialIn",
                Parameters = parameters,
                Variables = variables
            };
            ClientResponse result = WcfClient.ExecuteCmd(cmd);
            accResult.Result = result.Result;
            accResult.Message = result.Message;
            return accResult;
        }

        public virtual ClientResponse MaterialOut(string fixtureNum,int portNo)
        {
            ClientResponse accResult = new ClientResponse();
            if (AccClient == null || WcfClient == null || !IsConnected)
            {
                accResult.Result = false;
                accResult.Message = "ACC Server disconnected";
                return accResult;
            }
            Dictionary<string, object> variables = new Dictionary<string, object>();
            Dictionary<string, object> parameters = new Dictionary<string, object>();

            variables.Add("fixtureNum", fixtureNum);
            variables.Add("portNo", portNo);
            variables.Add("nomalMaterial", !UISession.ClientSetting.IsCoreMaterial);
            parameters.Add("Line", AccClient.Line);
            parameters.Add("OP", AccClient.OP);
            parameters.Add("STN", AccClient.STN);
            ClientRequest cmd = new ClientRequest
            {
                CmdName = "MaterialOut",
                Parameters = parameters,
                Variables = variables
            };
            ClientResponse result = WcfClient.ExecuteCmd(cmd);
            accResult.Result = result.Result;
            accResult.Message = result.Message;
            return accResult;
        }

        public virtual ClientResponse LotChange(string ItemPartNo, string Lot, string USN, string SLot, string Qty)
        {
            ClientResponse accResult = new ClientResponse();
            if (AccClient == null || WcfClient == null || !IsConnected)
            {
                accResult.Result = false;
                accResult.Message = "ACC Server disconnected";
                return accResult;
            }
            Dictionary<string, object> variables = new Dictionary<string, object>();
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("ItemPartNo", ItemPartNo);
            parameters.Add("Lot", Lot);
            parameters.Add("USN", USN);
            parameters.Add("SLot", SLot);
            parameters.Add("Qty", Qty);
            parameters.Add("Line", AccClient.Line);
            parameters.Add("OP", AccClient.OP);
            parameters.Add("STN", AccClient.STN);
            ClientRequest cmd = new ClientRequest
            {
                CmdName = "lot",
                Parameters = parameters,
                Variables = variables
            };
            ClientResponse result = WcfClient.ExecuteCmd(cmd);
            accResult.Result = result.Result;
            accResult.Message = result.Message;
            accResult.Data = result.Data;
            return accResult;
        }

        public virtual DateTime GetDateTime()
        {
            if (!IsConnected || AccClient == null)
            {
                return DateTime.MinValue;
            }

            Dictionary<string, object> variables = new Dictionary<string, object>();
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("Line", AccClient.Line);
            parameters.Add("OP", AccClient.OP);
            parameters.Add("STN", AccClient.STN);
            ClientRequest cmd = new ClientRequest
            {
                CmdName = "GetDateTime",
                Parameters = parameters,
                Variables = variables
            };
            ClientResponse result = WcfClient.ExecuteCmd(cmd);
            if (result.Result)
            {
                DateTime dt;
                DateTime.TryParse(result.Data.ToString(), out dt);
                return dt;
            }

            return DateTime.MinValue;
        }

        /// <summary>
        /// 传递事件到子类，方便子类激发事件
        /// </summary>
        /// <param name="processDatas"></param>
        protected virtual void SendOnReadProcessDataEvent(List<ClientProcData> processDatas)
        {
            if (OnReadProcessData != null)
            {
                OnReadProcessData(processDatas);
            }
        }

        /// <summary>
        /// NextStep请求
        /// </summary>
        /// <param name="barcodes"></param>
        /// <param name="partTypeId"></param>
        /// <returns></returns>
        public virtual ClientResponse NextStep(List<string> barcodes,int partTypeId)
        {
            ClientResponse accResult = new ClientResponse();
            if (AccClient == null || WcfClient == null || !IsConnected)
            {
                accResult.Result = false;
                accResult.Message = "ACC Server disconnected";
                return accResult;
            }
            Dictionary<string, object> variables = new Dictionary<string, object>();
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("Barcodes", barcodes);
            parameters.Add("Line", AccClient.Line);
            parameters.Add("OP", AccClient.OP);
            parameters.Add("STN", AccClient.STN);
            parameters.Add("PartTypeId", partTypeId);

            ClientRequest cmd = new ClientRequest
            {
                CmdName = "NextStep",
                Parameters = parameters,
                Variables = variables
            };
            Log4Helper.Info(JsonHelper.ToJson(cmd));
            ClientResponse result = WcfClient.ExecuteCmd(cmd);
            Log4Helper.Info(JsonHelper.ToJson(result));
            accResult = new ClientResponse();
            accResult.Result = result.Result;
            accResult.Message = result.Message;
            accResult.Data = result.Data;          
            return accResult;
        }

         
        /// <summary>
        /// Preload请求
        /// </summary>
        /// <param name="barcodes"></param>
        /// <param name="idname"></param>
        /// <param name="partTypeId"></param>
        /// <returns></returns>
        public virtual ClientResponse PreLoad(List<string> barcodes,string idname ,int partTypeId)
        {
            ClientResponse accResult = new ClientResponse();
            if (AccClient == null || WcfClient == null || !IsConnected)
            {
                accResult.Result = false;
                accResult.Message = "ACC Server disconnected";
                return accResult;
            }
            Dictionary<string, object> variables = new Dictionary<string, object>();
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("Barcodes", barcodes);
            parameters.Add("Line", AccClient.Line);
            parameters.Add("OP", AccClient.OP);
            parameters.Add("STN", AccClient.STN);
            parameters.Add("PartTypeId", partTypeId);
            parameters.Add("IDName", idname);

            ClientRequest cmd = new ClientRequest
            {
                CmdName = "PreLoad",
                Parameters = parameters,
                Variables = variables
            };
            Log4Helper.Info(JsonHelper.ToJson(cmd));
            ClientResponse result = WcfClient.ExecuteCmd(cmd);
            Log4Helper.Info(JsonHelper.ToJson(result));
            accResult = new ClientResponse();
            accResult.Result = result.Result;
            accResult.Message = result.Message;
            accResult.Data = result.Data;          

            return accResult;
        }

        /// <summary>
        /// 处理物料条码
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public virtual ClientResponse ProcessMaterialCode(string code)
        {
            ClientResponse accResult = new ClientResponse();
            if (AccClient == null || WcfClient == null || !IsConnected)
            {
                accResult.Result = false;
                accResult.Message = "ACC Server disconnected";
                return accResult;
            }
            Dictionary<string, object> variables = new Dictionary<string, object>();
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("MaterialCode", code);
            parameters.Add("Line", AccClient.Line);
            parameters.Add("OP", AccClient.OP);
            parameters.Add("STN", AccClient.STN);

            ClientRequest cmd = new ClientRequest
            {
                CmdName = "ProcessMaterialCode",
                Parameters = parameters,
                Variables = variables
            };
            Log4Helper.Info(JsonHelper.ToJson(cmd));
            ClientResponse result = WcfClient.ExecuteCmd(cmd);
            Log4Helper.Info(JsonHelper.ToJson(result));
            accResult = new ClientResponse();
            accResult.Result = result.Result;
            accResult.Message = result.Message;
            accResult.Data = result.Data;
            return accResult;
        }

        #region 事件

        public delegate void PreLoadHandler(List<string> barcodes, ClientResponse result);
        public event PreLoadHandler OnPreLoad;

        public delegate void LoadHandler(List<string> barcodes, ClientResponse result);
        public event LoadHandler OnLoad;

        public delegate void UnloadHandler(List<string> barcodes, ClientResponse result);
        public event UnloadHandler OnUnload;

        public delegate void ReadProcessDataHandler(List<ClientProcData> processDatas);
        public event ReadProcessDataHandler OnReadProcessData;
        #endregion
    }
}
